package pers.hl.module_main.common

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.TextView
import per.goweii.anylayer.DialogLayer
import per.goweii.anylayer.Layer
import pers.hl.module_main.R

class ConfirmDialog(context: Context?) : DialogLayer(context) {

    companion object {
        fun create(context: Context?): ConfirmDialog {
            val dialog = ConfirmDialog(context)
            dialog.contentView(R.layout.layout_confirm_dialog)
                .cancelableOnTouchOutside(true)
                .backgroundDimAmount(0.5f)
                .gravity(Gravity.CENTER)
            return dialog
        }
    }

//    override fun onCreateInAnimator(view: View?): Animator {
//        return AnimatorHelper.createZoomInAnim(view)
//    }
//
//    override fun onCreateOutAnimator(view: View?): Animator {
//        return AnimatorHelper.createZoomOutAnim(view)
//    }

    fun title(title: String): ConfirmDialog {
        bindData {
            val tvTitle = it.getView<TextView>(R.id.tvTitle)
            tvTitle?.text = title
        }
        return this
    }

    fun content(content: String): ConfirmDialog {
        bindData {
            val tvContent = it.getView<TextView>(R.id.tvContent)
            tvContent?.text = content
        }
        return this
    }

    fun setConfirmListener(confirmBlock: () -> Unit): ConfirmDialog {
        bindData { layer ->
            val tvConfirm = layer?.getView<TextView>(R.id.tvConfirm)
            tvConfirm?.setOnClickListener(View.OnClickListener {
                dismiss()
                confirmBlock.invoke()
            })
        }
        return this
    }

    fun setCancelListener(cancelBlock: () -> Unit): ConfirmDialog {
        bindData {
            val tvCancel = it.getView<TextView>(R.id.tvCancel)
            tvCancel?.setOnClickListener(View.OnClickListener {
                dismiss()
                cancelBlock.invoke()
            })
        }
        return this
    }

}